import os
import time

from django.conf import settings
from django.db import models

from mars.utils.models import BaseModel

# Create your models here.

def modify_path(instance, filename):
    """
    重定义图片保存路径
    :param instance: self
    :param filename: 文件名
    :return: 新路径
    """
    ext = filename.split(".").pop()
    now_time = int(time.time())
    filename = "{0}.{1}".format(now_time, ext)
    return os.path.join("ad_img", filename)  # 系统路径分隔符差异，增强代码重用性
class Advertisement(BaseModel):
    """广告模块"""

    # 广告位置
    position = models.PositiveSmallIntegerField(
        verbose_name="广告位",
        choices=[(1, "启动页"), (2, "首页"), (3, "收银台"), (5, "等待处理页")],
    )
    # 广告类型 小程序/自定义
    source = models.PositiveSmallIntegerField(
        verbose_name="广告源", choices=[(1, "微信"),( 2, "自定义")]
    )
    # 广告id    对应小程序申请的值
    adid = models.CharField(
        verbose_name="广告ID", help_text="微信广告需要设置", max_length=128, null=True, blank=True
    )
    # 广告文字
    title = models.CharField(verbose_name="标题", max_length=128, null=True, blank=True)
    # 广告图片
    img = models.ImageField(verbose_name="展示图片", upload_to=modify_path, null=True, blank=True)
    # 起始日期
    begin_date = models.DateField(verbose_name="起始日期",null=True, blank=True)
    # 结束日期
    end_date = models.DateField(verbose_name="结束日期",null=True, blank=True)

    class Meta:
        db_table = "tb_advertisment"
        verbose_name = "广告设置"
        verbose_name_plural = verbose_name
